﻿using System;
using System.Collections.Generic;
using Caliburn.Micro;
using SS.WPF.Framework.Localization;

namespace SS.WPF.Framework.Restriction
{
    public class RangeRestriction : Restriction<int>
    {
        public ulong? Maximum { get; set; }
        public int? Minimum { get; set; }

        public override bool Validate(int value)
        {
            var valid = true;
            if (Maximum != null)
            {
                valid = (ulong)Math.Max(value, 0) <= Maximum.Value;
            }

            if (Minimum != null)
            {
                valid = valid && value >= Minimum;
            }
            return valid;
        }

        public override string Description
        {
            get
            {
                var list = new List<string>();
                if (Minimum != null)
                {
                    list.Add(string.Format((string)IoC.Get<TranslationManager>().Translate("Minimum"), Minimum.Value));
                }

                if (Maximum != null)
                {
                    list.Add(string.Format((string)IoC.Get<TranslationManager>().Translate("Maximum"), Maximum.Value));
                }

                return string.Join("\t", list.ToArray());
            }
        }

        public override bool CanParse(string text)
        {
            int value;
            return int.TryParse(text, out value);
        }
    }

    public class UnsignedRangeRestriction : Restriction<ulong>
    {
        public ulong? Maximum { get; set; }
        public ulong? Minimum { get; set; }

        public override bool Validate(ulong value)
        {
            var valid = true;
            if (Maximum != null)
            {
                valid = (ulong)Math.Max((int)value, 0) <= Maximum.Value;
            }

            if (Minimum != null)
            {
                valid = valid && value >= Minimum;
            }
            return valid;
        }

        public override string Description
        {
            get
            {
                var list = new List<string>();
                if (Minimum != null)
                {
                    list.Add(string.Format((string)IoC.Get<TranslationManager>().Translate("Minimum"), Minimum.Value));
                }

                if (Maximum != null)
                {
                    list.Add(string.Format((string)IoC.Get<TranslationManager>().Translate("Maximum"), Maximum.Value));
                }

                return string.Join("\t", list.ToArray());
            }
        }

        public override bool CanParse(string text)
        {
            ulong value;
            return ulong.TryParse(text, out value);
        }
    }

    public class DoubleRangeRestriction : Restriction<double>
    {
        public DoubleRangeRestriction()
        {
            Minimum = int.MinValue;
            Maximum = int.MaxValue;
        }

        public double? Maximum { get; set; }
        public double? Minimum { get; set; }

        public override bool Validate(double value)
        {
            var valid = true;
            if (Maximum != null)
            {
                valid = value <= Maximum.Value;
            }

            if (Minimum != null)
            {
                valid = valid && value >= Minimum;
            }
            return valid;
        }

        public override string Description
        {
            get
            {
                var list = new List<string>();
                if (Minimum != null)
                {
                    list.Add(string.Format((string)IoC.Get<TranslationManager>().Translate("Minimum"), Minimum.Value));
                }

                if (Maximum != null)
                {
                    list.Add(string.Format((string)IoC.Get<TranslationManager>().Translate("Maximum"), Maximum.Value));
                }

                return string.Join("\t", list.ToArray());
            }
        }

        public override bool CanParse(string text)
        {
            double value;
            return double.TryParse(text, out value);
        }
    }
}